package com.wm.web.action;

import java.util.Date;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.wm.bean.Shopkeeper;
import com.wm.bean.User;
import com.wm.comm.Code;
import com.wm.comm.Constant;
import com.wm.comm.EmailUtil;
import com.wm.comm.MD5;
import com.wm.service.ShopkeeperService;
import com.wm.service.UserService;
import com.wm.web.interceptor.RemoteAddrAware;
import com.wm.web.interceptor.ShopkeeperSessionAware;
import com.wm.web.servlet.*;

public class ShopkeeperPassword extends BaseAction implements SessionAware,ShopkeeperSessionAware{
	private static final Log logger = LogFactory.getLog(PasswordAction.class);
	
	private ShopkeeperService shopkeeperService;

	public ShopkeeperService getShopkeeperService() {
		return shopkeeperService;
	}

	public void setShopkeeperService(ShopkeeperService shopkeeperService) {
		this.shopkeeperService = shopkeeperService;
	}
	
	private ShopkeeperSession shopkeeperSession;
	private Map session;
	
	public ShopkeeperSession getShopkeeperSession() {
		return shopkeeperSession;
	}

	public void setShopkeeperSession(ShopkeeperSession shopkeeperSession) {
		this.shopkeeperSession = shopkeeperSession;
	}

	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}
	
	private String password;
	private String oldPwd;
	private String result;
	private String username;
	private String verifycode;
	private String ticket;
	private String error;

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getVerifycode() {
		return verifycode;
	}

	public void setVerifycode(String verifycode) {
		this.verifycode = verifycode;
	}

	public String getTicket() {
		return ticket;
	}

	public void setTicket(String ticket) {
		this.ticket = ticket;
	}
	
	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return INPUT;
		}
	}

	/**
	 * 初始页
	 */
	public String index(){
		this.setAction("update");
		return "input";
	}
	
	/**
	 * 修改密码
	 * @return
	 */
	public String update(){
		if(this.getPassword()==null ||this.getPassword().trim().equals("")||this.getPassword().length()>20){
			this.addFieldError("error", this.getText("error.password"));
			this.setError("1");
			return "input";
		}
		
		if(!checkPassword(this.getPassword())){
			this.addFieldError("error", this.getText("error.password"));
			this.setError("1");
			return "input";
		}
		Shopkeeper sk = this.getShopkeeperService().findShopkeeperById(this.getShopkeeperSession().getSkid());
		MD5 md5 = new MD5();
		System.out.println(md5.getMD5ofStr(this.getOldPwd()));
		if(sk!=null && sk.getUserPwd().equals(md5.getMD5ofStr(this.getOldPwd()))){
			sk.setUserPwd(md5.getMD5ofStr(this.getPassword()));
			sk.setUserPwdShow(this.getPassword());
			this.getShopkeeperService().updateShopkeeper(sk);
			this.addFieldError("error", this.getText("error.password.ok"));
		}else{
			this.addFieldError("error", this.getText("error.oldPwd"));
		}
		this.setError("1");
		return "input";
	}
	
	public static boolean checkPassword(String password) {
		  //String regex = "([a-z]|[A-Z]|[0-9]|[\\u4e00-\\u9fa5])+";
		  String regex = "^[a-zA-Z0-9_]+$";
		  Pattern p = Pattern.compile(regex);
		  Matcher m = p.matcher(password);
		  return m.matches();
		}
}
